package day04;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class CountWords {


    public static void main(String[] args) {
        String[] words = "   To know  everything  is to know   nothing   "
                .trim()
                .toLowerCase()
                .replaceAll("[^a-z]", " ")
                .split("\\s+");
        System.out.println(Arrays.asList(words));
        Map<String, Integer> map = new TreeMap<>();
        for (String word : words) {
            if (map.containsKey(word)) {//map.get(word)!=null
                map.put(word, map.get(word) + 1);
            } else {
                map.put(word, 1);
            }
        }
        System.out.println(map);
        //poor
        for (String key : map.keySet()) {
            System.out.println(key + " " + map.get(key));
        }
        //better
        Iterator<Map.Entry<String,Integer>> it=map.entrySet().iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
}